package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CorePartner;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CorePartnerService;
import com.xiaoyu.lingdian.vo.CorePartnerVO;

@Controller
@RequestMapping(value="/corePartner")
public class CorePartnerController extends BaseController {

	/**
	* CorePartner表
	*/
	@Autowired
	private CorePartnerService corePartnerService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	* 添加
	*
	* @param crprrName 名称
	* @param crprrCode 内容
	* @param uuids
	* @return
	*/
	@RequestMapping(value="/add/corePartner", method=RequestMethod.POST)
	public void addCorePartner (String crprrName, String crprrCode, String uuids, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin addCorePartner");

		if (StringUtil.isEmpty(crprrName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[名称]不能为空!"), response);
			return;
		}
				CorePartner corePartner = new CorePartner();
		String uuid = RandomUtil.generateString(16);
		corePartner.setCrprrUuid(uuid);
		corePartner.setCrprrName(crprrName);
		corePartner.setCrprrCode(crprrCode);
		corePartner.setCrprrCdate(new Date());
		corePartner.setCrprrUdate(new Date());

		corePartnerService.insertCorePartner(corePartner);

		// 添加管家伙伴封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(uuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CorePartnerController]:end addCorePartner");

	}

	/**
	* 修改
	*
	* @param crprrUuid 标识UUID
	* @param crprrName 名称
	* @param crprrCode 内容
	* @param uuids
	* @return
	*/
	@RequestMapping(value="/update/corePartner", method=RequestMethod.POST)
	public void updateCorePartner (String crprrUuid, String crprrName, String crprrCode, String uuids, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin updateCorePartner");
		if (StringUtil.isEmpty(crprrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CorePartner corePartner = new CorePartner();
		corePartner.setCrprrUuid(crprrUuid);
		corePartner.setCrprrName(crprrName);
		corePartner.setCrprrCode(crprrCode);
		corePartner.setCrprrUdate(new Date());

		corePartnerService.updateCorePartner(corePartner);
		
		// 修改管家伙伴封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(crprrUuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CorePartnerController]:end updateCorePartner");

	}

	/**
	* 删除
	*
	* @param crprrUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCorePartner (String crprrUuid, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin deleteCorePartner");
		if (StringUtil.isEmpty(crprrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CorePartner corePartner = new CorePartner();
		corePartner.setCrprrUuid(crprrUuid);

		corePartnerService.deleteCorePartner(corePartner);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CorePartnerController]:end deleteCorePartner");

	}

	/**
	* 批量删除
	*
	* @param crprrUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCorePartner (String crprrUuids, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin deleteBatchCorePartner");
		if (StringUtil.isEmpty(crprrUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crprrUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		corePartnerService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CorePartnerController]:end deleteBatchCorePartner");

	}

	/**
	* 获取单个
	*
	* @param crprrUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCorePartner (String crprrUuid, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin viewsCorePartner");
		if (StringUtil.isEmpty(crprrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CorePartner corePartner = new CorePartner();
		corePartner.setCrprrUuid(crprrUuid);

		corePartner = corePartnerService.getCorePartner(corePartner);

		CorePartnerVO corePartnerVO = new CorePartnerVO();
		corePartnerVO.convertPOToVO(corePartner);

		//根据管家伙伴UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(crprrUuid);
		corePartnerVO.setList(attachmentslist);
				writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", corePartnerVO), response);
		logger.info("[CorePartnerController]:end viewsCorePartner");

	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCorePartnerList (HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin findCorePartnerList");
		List<CorePartner> lists = corePartnerService.findCorePartnerList();
		List<CorePartnerVO> vos = new ArrayList<CorePartnerVO>();
		CorePartnerVO vo;
		for (CorePartner corePartner : lists) {
			vo = new CorePartnerVO();
			vo.setCrprrUuid(corePartner.getCrprrUuid());
			vo.setCrprrName(corePartner.getCrprrName());
			vo.setCrprrCode(corePartner.getCrprrCode());
			vo.setCrprrCdate(DateUtil.formatTimesTampDate(corePartner.getCrprrCdate()));
			vo.setCrprrUdate(DateUtil.formatTimesTampDate(corePartner.getCrprrUdate()));
			//根据管家伙伴UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePartner.getCrprrUuid());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CorePartnerController]:end findCorePartnerList");

	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCorePartnerPage (Integer pageNum, Integer pageSize, String crprrName, HttpServletResponse response) {
		logger.info("[CorePartnerController]:begin findCorePartnerPage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CorePartner> page = corePartnerService.findCorePartnerPage(pageNum, pageSize, crprrName);
		Page<CorePartnerVO> pageVO = new Page<CorePartnerVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CorePartnerVO> vos = new ArrayList<CorePartnerVO>();
		CorePartnerVO vo;
		for (CorePartner corePartner : page.getResult()) {
			vo = new CorePartnerVO();
			vo.setCrprrUuid(corePartner.getCrprrUuid());
			vo.setCrprrName(corePartner.getCrprrName());
			vo.setCrprrCode(corePartner.getCrprrCode());
			vo.setCrprrCdate(DateUtil.formatTimesTampDate(corePartner.getCrprrCdate()));
			vo.setCrprrUdate(DateUtil.formatTimesTampDate(corePartner.getCrprrUdate()));
			//根据管家伙伴UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePartner.getCrprrUuid());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CorePartnerController]:end findCorePartnerPage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}